#coding=utf-8
from uliweb import expose

#可以省略直接引用
from uliweb import request, error, redirect
from sqlalchemy.sql import or_

def __begin__():
    """
    用户验证 权限验证
    """
    from uliweb import functions
    functions.require_login()
    return functions.has_role()
 
@expose('/menu')
class Menu(object):

    
    def __init__(self):
        from uliweb.orm import get_model
        self.menumodel = get_model('menu')

    def __begin__(self):
        if not request.user.is_superuser:
            error('你不是超级用户不能进行这项操作！')         
    
    def menu(self):
        """
        菜单管理首页
        """
        return {}
    
    def menuedit(self):
        """
        编辑菜单
        """
        _id = request.GET.get("id")
        #如果_id不存在，就是url非法输入，就提示错误
        if _id == None or _id == "":
            return json({"status":500,"msg":"URL错误"})
        #从redis获取数据
        ormdata = functions.get_object(self.menumodel, _id, cache=True, use_local=True)

        #将对象传给模版
        return {"ormdata":ormdata}    
    
    def menuadd(self):
        """
        菜单添加
        """

        
        return {} 
        
    def menusave(self):
        """
        保存编辑菜单
        """
        _id = request.POST.get("id")
        menuname = request.POST.get("menuname")
        menuurl = request.POST.get("menuurl")
        #menupid = request.POST.get("menupid")
        menuicon = request.POST.get("menuicon")
        menuseq = request.POST.get("menuseq")
        menupath = request.POST.get("menupath")
        menutype = request.POST.get("menutype")

        #menupath的格式 0,1,2 最后一位是菜单的上级菜单
        if "," in menupath:
            menupid = menupath.split(",")[-1]
        else:
            menupid = menupath
            
        if _id == None:
            menu = self.menumodel(menuname=menuname,
                          menuurl=menuurl,
                          menupid=menupid,
                          menuicon=menuicon,
                          menutype=menutype,
                          menupath = menupath,
                          menuseq=menuseq
                          )
            
        else:
            menu = functions.get_object(self.menumodel, _id, cache=True, use_local=True)
            #update操作
            menu.menuname=menuname
            menu.menuurl=menuurl
            menu.menupid=menupid
            menu.menuicon=menuicon
            menu.menupath=menupath
            menu.menutype=menutype
            menu.menuseq=menuseq

        
        try:
            menu.save()
            return json({"status":200,"msg":"保存成功"})
        
        except:
            #获取错误信息
            #sys.exc_info()的返回值是一个tuple, (type, value/message, traceback)
            #这里的type ---- 异常的类型
            #value/message ---- 异常的信息或者参数
            #traceback ---- 包含调用栈信息的对象。            
            tp,val,td = sys.exc_info()

            return json({"status":500,"msg":val})
        
    def menudel(self):
        """
        删除菜单
        """
                
        if request.method == 'GET':
            error("你访问的页面不存在！请联系管理员")
    
        _id = request.POST.get('id')
    
        mid = []
        for i in _id.split(','):
            mid.append(int(i))       
    
    
        try:
            self.menumodel.filter(self.menumodel.c.id.in_(mid)).remove()
    
            d = {"status":200,"msg":"删除成功"}
        except:
            d = {"status":500,"msg":"删除失败"}
    
        return json(d)        
        #一次删除所有数据

        #self.menumodel.filter(or_(self.menumodel.c.pid == _id,self.menumodel.c.id == _id)).remove()
        

        